package cn.wolfcode.edu.web.controller;

import cn.wolfcode.edu.anno.PermissionName;
import cn.wolfcode.edu.domain.Department;
import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.domain.Role;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IDepartmentService;
import cn.wolfcode.edu.util.JsonResult;
import cn.wolfcode.edu.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by abb on 2018-07-08.
 */
@RequestMapping("department")
@Controller
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    @RequestMapping("")
    public String index(){
        return "/department";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return departmentService.query(qo);
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Department> listAll() {
        return departmentService.selectAll();
    }

   @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Department department){
        JsonResult jsonResult = new JsonResult();
        try {
            departmentService.insert(department);

        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("get wrong");
        }
        return jsonResult;
    }
    //提交保存员工 { success:true,msg:""}
    @RequestMapping("update")
    @ResponseBody

    public JsonResult update(Department department){
        JsonResult jsonResult = new JsonResult();
        try {
            departmentService.updateByPrimaryKey(department);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg("get wrong");
        }
        return jsonResult;
    }
    /*@RequestMapping("save")
    @ResponseBody
    public JsonResult save(Department department){
        JsonResult result = new JsonResult();
        try{
            departmentService.insert(department);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    public JsonResult edit(Department department){
        JsonResult result = new JsonResult();
        try{
            departmentService.updateByPrimaryKey(department);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }*/

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try{
            departmentService.deleteByPrimaryKey(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }

    @RequestMapping("close")
    @ResponseBody
    public JsonResult close(Long id){
        JsonResult result = new JsonResult();
        try{
            departmentService.close(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }
/*    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Department department){
        JsonResult result = new JsonResult();
        try{
            departmentService.insert(department);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Department department){
        JsonResult result = new JsonResult();
        try{
            departmentService.updateByPrimaryKey(department);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }*/


}
